package sk.stuba.fiit.foo07.genex.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.util.ArrayList;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

import sk.stuba.fiit.foo07.genex.beans.Keyword;
import sk.stuba.fiit.foo07.genex.dao.KeywordDao;
import sk.stuba.fiit.foo07.genex.dao.KeywordDaoDerby;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class NewKeywordDialog extends javax.swing.JDialog {

    /**
     * 
     */
    private static final long serialVersionUID = 3299084890496830893L;
    private DefaultListModel keywords;
    private KeywordDao kDao;
    private Keyword[] ret;
    private JLabel jLabel2;
    private JButton jBtnNew;

    private JTextField jTxtKeyword;
    private JButton jBtnOK;
    private JButton jBtnCancel;
    private JSeparator jSeparator1;
    private JList jLstKeywords;
    private JScrollPane jScrKeywords;
    private JLabel jLabel1;

    public NewKeywordDialog(JDialog frame, Connection c) {
        super(frame);

        keywords = new DefaultListModel();
        try {
            kDao = new KeywordDaoDerby(c);
        } catch (Exception e) {
        }

        ArrayList<Keyword> keys = null;
        //TODO
        //pozret
        try {
            keys = kDao.getAllKeywords();
        } catch (Exception e) {
        }
        for (Keyword k : keys)
            keywords.addElement(k);

        initGUI();
    }

    private void initGUI() {
        try {
            {
                this.setTitle("Prida\u0165 k\u013eú\u010dové slová");
                getContentPane().setLayout(null);
                this.setResizable(false);
                this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
                setLocationByPlatform(true);
                {
                    jLabel1 = new JLabel();
                    getContentPane().add(jLabel1);
                    jLabel1.setText("Nové");
                    jLabel1.setBounds(12, 12, 55, 14);
                    jLabel1.setForeground(new java.awt.Color(100, 0, 0));
                }
                {
                    jTxtKeyword = new JTextField();
                    getContentPane().add(jTxtKeyword);
                    jTxtKeyword.setBounds(79, 9, 161, 21);
                    jTxtKeyword
                            .setBackground(new java.awt.Color(255, 255, 240));
                    jTxtKeyword.addCaretListener(new CaretListener() {
                        public void caretUpdate(CaretEvent evt) {
                            jTxtKeywordCaretUpdate(evt);
                        }
                    });
                }
                {
                    jScrKeywords = new JScrollPane();
                    getContentPane().add(jScrKeywords);
                    jScrKeywords.setBounds(12, 59, 228, 159);
                    {
                        jLstKeywords = new JList();
                        jScrKeywords.setViewportView(jLstKeywords);
                        jLstKeywords.setModel(keywords);
                        jLstKeywords.setBackground(new java.awt.Color(255, 255,
                                240));
                        jLstKeywords.addMouseListener(new MouseAdapter() {
                            @Override
                            public void mouseClicked(MouseEvent evt) {
                                if (evt.getClickCount() == 2)
                                    jBtnOKActionPerformed(new ActionEvent(
                                            jBtnOK, 0, null));
                            }
                        });
                    }
                }
                {
                    jSeparator1 = new JSeparator();
                    getContentPane().add(jSeparator1);
                    jSeparator1.setBounds(12, 224, 228, 10);
                }
                {
                    jBtnCancel = new JButton();
                    getContentPane().add(jBtnCancel);
                    jBtnCancel.setText("Cancel");
                    jBtnCancel.setBounds(155, 232, 85, 21);
                    jBtnCancel.setMnemonic(KeyEvent.VK_C);
                    jBtnCancel.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent evt) {
                            jBtnCancelActionPerformed(evt);
                        }
                    });
                }
                {
                    jBtnOK = new JButton();
                    getContentPane().add(jBtnOK);
                    jBtnOK.setText("OK");
                    jBtnOK.setBounds(59, 232, 85, 21);
                    jBtnOK.setMnemonic(KeyEvent.VK_O);
                    jBtnOK.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent evt) {
                            jBtnOKActionPerformed(evt);
                        }
                    });
                }
                {
                    jBtnNew = new JButton();
                    getContentPane().add(jBtnNew);
                    jBtnNew.setText("v");
                    jBtnNew.setBounds(200, 34, 40, 21);
                    jBtnNew.setMnemonic(java.awt.event.KeyEvent.VK_V);
                    jBtnNew.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent evt) {
                            jBtnNewActionPerformed(evt);
                        }
                    });
                }
                {
                    jLabel2 = new JLabel();
                    getContentPane().add(jLabel2);
                    jLabel2.setText("Výber k\u013eú\u010dových slov");
                    jLabel2.setBounds(12, 36, 164, 17);
                    jLabel2.setForeground(new java.awt.Color(100, 0, 0));
                }
            }
            this.setSize(264, 300);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Keyword[] showDialog() {
        this.setModalityType(ModalityType.DOCUMENT_MODAL);
        setVisible(true);
        return ret;
    }

    private void jBtnOKActionPerformed(ActionEvent evt) {
        int[] ind = jLstKeywords.getSelectedIndices();
        ret = new Keyword[ind.length];
        for (int i = 0; i < ind.length; i++)
            ret[i] = (Keyword) keywords.elementAt(ind[i]);

        setVisible(false);
    }

    private void jBtnCancelActionPerformed(ActionEvent evt) {
        ret = null;
        setVisible(false);
    }

    private void jTxtKeywordCaretUpdate(CaretEvent evt) {
        jLstKeywords.clearSelection();
        jLstKeywords.ensureIndexIsVisible(0);
        if (jTxtKeyword.getText().length() == 0)
            return;

        int i, size = keywords.getSize();
        for (i = 0; i < size; i++) {
            if (keywords.get(i).toString().startsWith(jTxtKeyword.getText())) {
                jLstKeywords.addSelectionInterval(i, i);
            }
        }
        jLstKeywords.ensureIndexIsVisible(jLstKeywords.getSelectedIndex());
    }

    private boolean hasKeyword() {
        for (int i = 0; i < keywords.getSize(); i++)
            if (jTxtKeyword.getText().equals(keywords.get(i).toString()))
                return true;

        return false;
    }

    private void jBtnNewActionPerformed(ActionEvent evt) {
        if (jTxtKeyword.getText().trim().length() == 0)
            return;
        if (jTxtKeyword.getText().trim().length() > 50) {
            JOptionPane.showMessageDialog(this,
                    "Kľúčové slovo môže obsahovať maximálne 50 znakov.",
                    "Chyba", JOptionPane.ERROR_MESSAGE);
            return;
        }

        if (hasKeyword()) {
            JOptionPane.showMessageDialog(this,
                    "Dané kľúčové slovo sa už v databáze nachádza.", "Chyba",
                    JOptionPane.ERROR_MESSAGE);
            return;
        }

        keywords.addElement(new Keyword(jTxtKeyword.getText().trim(), null));
        jTxtKeyword.requestFocusInWindow();
        jTxtKeywordCaretUpdate(null);
    }

    public void deactivateNewKeywordAdding() {
        jBtnNew.setEnabled(false);
    }
}
